package org.btik.light.mobile.ui.factory;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import org.btik.context.SimpleContext;
import org.btik.light.mobile.common.handler.DevBinder;
import org.btik.light.mobile.meta.mo.DefaultDevMo;

import static org.btik.light.mobile.platform.conf.ContextGlobalNames.CURRENT_DEV;

/**
 * Activity类型的设备面板
 */
public abstract class DevPanelActivity extends Activity {

    protected DefaultDevMo defaultDevMo;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        defaultDevMo = SimpleContext.get(CURRENT_DEV);
    }

    protected void bindDev(Object actionListener) {
        if (actionListener instanceof DevBinder) {
            ((DevBinder) actionListener).bind(defaultDevMo);
        }
    }

    protected <T extends View> T bind(@IdRes int id, View.OnClickListener onClickListener) {
        T viewById = findViewById(id);
        viewById.setOnClickListener(onClickListener);
        bindDev(onClickListener);
        return viewById;
    }

    @SuppressLint("ClickableViewAccessibility")
    protected <T extends View> T bind(@IdRes int id, View.OnTouchListener onTouchListener) {
        T viewById = findViewById(id);
        viewById.setOnTouchListener(onTouchListener);
        bindDev(onTouchListener);
        return viewById;
    }

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    protected Switch bind(@IdRes int id, CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        Switch aSwitch = findViewById(id);
        aSwitch.setOnCheckedChangeListener(onCheckedChangeListener);
        bindDev(onCheckedChangeListener);
        return aSwitch;
    }

    protected SeekBar bind(@IdRes int id, SeekBar.OnSeekBarChangeListener onSeekBarChangeListener) {
        SeekBar seekBar = findViewById(id);
        seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
        bindDev(onSeekBarChangeListener);
        return seekBar;
    }
}
